package top.xizai.bean.factprybean;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.stereotype.Component;
import top.xizai.bean.entity.Student;

/**
 * @NAME: WSC
 * @DATE: 2021/12/13
 * @DESCRIBE:
 **/
@Component
public class MyStudentFactoryBean implements FactoryBean<Student> {
    @Override
    public Student getObject() throws Exception {
        var enhancer = new Enhancer();
        enhancer.setSuperclass(Student.class);
        enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
            System.out.println("======插入前置通知======");
            Object object = methodProxy.invokeSuper(o, objects);
            System.out.println("======插入后置通知======");
            return object;
        });
        var o = enhancer.create();

        return (Student) o;
    }

    @Override
    public Class<?> getObjectType() {
        return Student.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
